using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;

namespace OgilvyOne.SqlServerDAL
{
    public class RoleUser : IRoleUser
    {
        // Fields
        private const string PARM_ISDEL = "@IsDel";
        private const string PARM_ROLE_UID = "@Role_UID";
        private const string PARM_ROLE_USER_UID = "@SRU_UID";
        private const string PARM_USER_UID = "@User_UID";
        private const string SQL_DELETE_ROLE_USER = "UPDATE Sys_Role_User SET IsDel = '1' WHERE SRU_UID = @SRU_UID";
        private const string SQL_DELETE_ROLE_USER_UID = "UPDATE Sys_Role_User SET IsDel = '1' WHERE User_UID = @User_UID";
        private const string SQL_INSERT_ROLE_USER = "INSERT INTO Sys_Role_User VALUES(@Role_UID,@User_UID,'0')";
        private const string SQL_SELECT_ROLE_USER = "SELECT SRU_UID,Role_UID,User_UID FROM Sys_Role_User  where SRU_UID=@SRU_UID and IsDel='0'";
        private const string SQL_SELECT_ROLE_USER_LIST = "SELECT SRU_UID,Role_UID,User_UID FROM Sys_Role_User where IsDel='0'";
        private const string SQL_SELECT_ROLE_USER_UID = "SELECT SRU_UID,Role_UID,User_UID FROM Sys_Role_User  where User_UID=@User_UID and IsDel='0'";
        private const string SQL_UPDATE_ROLE_USER = "UPDATE Sys_Role_User SET Role_UID = @Role_UID, User_UID = @User_UID WHERE SRU_UID = @SRU_UID";

        // Methods
        public void Delete(int roleuser_id)
        {
            SqlParameter[] roleUserDeleteParameters = GetRoleUserDeleteParameters();
            roleUserDeleteParameters[0].Value = roleuser_id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Role_User SET IsDel = '1' WHERE SRU_UID = @SRU_UID", roleUserDeleteParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        public void DeleteByUserID(int user_id)
        {
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@User_UID", SqlDbType.Int, 4, "User_UID") };
            cmdParms[0].Value = user_id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Role_User SET IsDel = '1' WHERE User_UID = @User_UID", cmdParms);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        public string getRoleByUserID(int userid)
        {
            string str = "0";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@User_UID", SqlDbType.Int, 4, "User_UID") };
            cmdParms[0].Value = userid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT SRU_UID,Role_UID,User_UID FROM Sys_Role_User  where User_UID=@User_UID and IsDel='0'", cmdParms))
            {
                while (reader.Read())
                {
                    str = str + "," + reader.GetInt32(1).ToString();
                }
            }
            return str;
        }

        public IList getRoleListByUserID(int userid)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@User_UID", SqlDbType.Int, 4, "User_UID") };
            cmdParms[0].Value = userid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT SRU_UID,Role_UID,User_UID FROM Sys_Role_User  where User_UID=@User_UID and IsDel='0'", cmdParms))
            {
                while (reader.Read())
                {
                    RoleUserInfo info = new RoleUserInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2));
                    list.Add(info);
                }
            }
            return list;
        }

        private static SqlParameter[] GetRoleUserDeleteParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Role_User SET IsDel = '1' WHERE SRU_UID = @SRU_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@SRU_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_Role_User SET IsDel = '1' WHERE SRU_UID = @SRU_UID", cachedParameters);
            }
            return cachedParameters;
        }

        private static SqlParameter[] GetRoleUserInsertParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("INSERT INTO Sys_Role_User VALUES(@Role_UID,@User_UID,'0')");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Role_UID", SqlDbType.Int, 4), new SqlParameter("@User_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("INSERT INTO Sys_Role_User VALUES(@Role_UID,@User_UID,'0')", cachedParameters);
            }
            return cachedParameters;
        }

        public IList getRoleUserList()
        {
            IList list = new ArrayList();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT SRU_UID,Role_UID,User_UID FROM Sys_Role_User where IsDel='0'", null))
            {
                while (reader.Read())
                {
                    RoleUserInfo info = new RoleUserInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getRoleUserList(int funcid)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SRU_UID", SqlDbType.Int, 4, "SRU_UID") };
            cmdParms[0].Value = funcid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT SRU_UID,Role_UID,User_UID FROM Sys_Role_User  where SRU_UID=@SRU_UID and IsDel='0'", cmdParms))
            {
                while (reader.Read())
                {
                    RoleUserInfo info = new RoleUserInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2));
                    list.Add(info);
                }
            }
            return list;
        }

        private static SqlParameter[] GetRoleUserModifyParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Role_User SET Role_UID = @Role_UID, User_UID = @User_UID WHERE SRU_UID = @SRU_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@SRU_UID", SqlDbType.Int, 4), new SqlParameter("@Role_UID", SqlDbType.Int, 4), new SqlParameter("@User_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_Role_User SET Role_UID = @Role_UID, User_UID = @User_UID WHERE SRU_UID = @SRU_UID", cachedParameters);
            }
            return cachedParameters;
        }

        public void Insert(RoleUserInfo acc)
        {
            SqlParameter[] roleUserInsertParameters = GetRoleUserInsertParameters();
            this.SetRoleUserInsertParameters(roleUserInsertParameters, acc);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "INSERT INTO Sys_Role_User VALUES(@Role_UID,@User_UID,'0')", roleUserInsertParameters);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        private void SetRoleUserInsertParameters(SqlParameter[] parms, RoleUserInfo acc)
        {
            parms[0].Value = acc.Role_UID;
            parms[1].Value = acc.User_UID;
        }

        private void SetRoleUserModifyParameters(SqlParameter[] parms, RoleUserInfo acc)
        {
            parms[0].Value = acc.SRU_UID;
            parms[1].Value = acc.Role_UID;
            parms[2].Value = acc.User_UID;
        }

        public void Update(RoleUserInfo myItem)
        {
            SqlParameter[] roleUserModifyParameters = GetRoleUserModifyParameters();
            this.SetRoleUserModifyParameters(roleUserModifyParameters, myItem);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Role_User SET Role_UID = @Role_UID, User_UID = @User_UID WHERE SRU_UID = @SRU_UID", roleUserModifyParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
